/**
 * 此文件是用来对支持一些后台请求的通用方方法
 */
import { Ref } from "vue";
import { ElMessageBox } from "element-plus";
import { messageInfoTip, messageSuccessTip, messageWarningTip } from "./message-config";
import { ServiceApi } from "../api/modules/_base";
import { IPageTableExpose } from "@whjxtech/jxtech-component";

export type PageTableDeleteConfig = {
  /**是否刷新表格 */
  refreshTable?: boolean;
  /**删除时是否携带额外参数 */
  deleteExtData?: any;
};

/**
 * 删除表格数据
 * @param PageTableRef 分页表格ref
 * @param servicelApi 请求api
 * @returns
 */
export function deletePageTableSelectionDataList(
  PageTableRef: Ref<IPageTableExpose>,
  servicelApi: ServiceApi,
  pageTableDeleteConfig?: PageTableDeleteConfig
) {
  console.log(PageTableRef);
  const selectionData = PageTableRef.value.getSelectionData();
  if (!selectionData || selectionData.length == 0) {
    messageWarningTip("没有选择需要删除的数据!");
    return;
  }
  ElMessageBox.confirm("此操作将永久删除选中的数据, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    lockScroll: false
  })
    .then(() => {
      servicelApi.saveMulti({ insert: [], update: [], delete: selectionData }, pageTableDeleteConfig?.deleteExtData).then(() => {
        messageSuccessTip("数据删除成功!");
        if (pageTableDeleteConfig?.refreshTable !== false) {
          PageTableRef.value.loadTable();
        }
      });
    })
    .catch(() => {
      // messageInfoTip("已取消删除!");
    });
}

/**
 * 删除表格数据
 * @param PageTableRef 分页表格ref
 * @param servicelApi 请求api
 * @returns
 */
export function deletePageTableSelectionPkList(
  PageTableRef: Ref<IPageTableExpose>,
  servicelApi: ServiceApi,
  pageTableDeleteConfig?: PageTableDeleteConfig,
  callback?: Function
) {
  const selectionPkList: string[] = PageTableRef.value.getSelectionPkList();
  if (!selectionPkList || selectionPkList.length == 0) {
    messageWarningTip("没有选择需要删除的数据!");
    return;
  }
  ElMessageBox.confirm("此操作将永久删除选中的数据, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    lockScroll: false
  })
    .then(() => {
      servicelApi.saveMultiDelete(selectionPkList, pageTableDeleteConfig?.deleteExtData).then(() => {
        messageSuccessTip("数据删除成功!");
        if (pageTableDeleteConfig?.refreshTable !== false) {
          PageTableRef.value.loadTable();
          if (callback) {
            callback();
          }
        }
      });
    })
    .catch(() => {
      // messageInfoTip("已取消删除!");
    });
}

/**
 * 删除表单单个数据
 * @param PageTableRef
 * @param servicelApi
 * @param pks
 * @param pageTableDeleteConfig
 * @param callback
 * @returns
 */
export function deletePageTablePk(
  PageTableRef: Ref<IPageTableExpose>,
  servicelApi: ServiceApi,
  pks: any[],
  pageTableDeleteConfig?: PageTableDeleteConfig,
  callback?: Function
) {
  if (!pks || pks.length == 0) {
    messageWarningTip("没有选择需要删除的数据!");
    return;
  }
  ElMessageBox.confirm("此操作将永久删除选中的数据, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    lockScroll: false
  })
    .then(() => {
      servicelApi.saveMultiDelete(pks, pageTableDeleteConfig?.deleteExtData).then(() => {
        messageSuccessTip("数据删除成功!");
        if (pageTableDeleteConfig?.refreshTable !== false) {
          PageTableRef.value.loadTable();
          if (callback) {
            callback();
          }
        }
      });
    })
    .catch(() => {
      // messageInfoTip("已取消删除!");
    });
}
